package org.ohuyo.libra.client.slave.webapp.actions;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.ohuyo.libra.client.exception.LibraClientException;
import org.ohuyo.libra.client.session.LibraSession;
import org.ohuyo.libra.client.util.LibraUtils;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * @author rabbit
 * 
 */
@Action("/slave/attribute")
@Result(name = "success", location = "/slave/attribute.jsp")
public class AttributeAction extends ActionSupport {

	private String master2SlaveKey = "master2SlaveKey";
	private String masterSharedKey = "masterSharedKey";
	private String master2SlaveDisposableKey = "master2SlaveDisposableKey";

	private String master2SlaveValue;
	private String masterSharedValue;
	private String master2SlaveDisposableValue;

	private static final String SLAVE_APP_CODE = "libraSlaveClient";
	private static final String MASTER_APP_CODE = "libraMasterClient";

	private String slave2MasterKey = "slave2MasterKey";
	private String slaveSharedKey = "slaveSharedKey";
	private String slave2MasterDisposableKey = "slave2MasterDisposableKey";

	private String slave2MasterValue;
	private String slaveSharedValue;
	private String slave2MasterDisposableValue;

	/**
	 * 
	 */
	private static final long serialVersionUID = -7956649549138837916L;

	public String execute() throws Exception {
		return super.execute();
	}

	public String cleanMasterAttr() throws LibraClientException {
		LibraSession s = LibraUtils.getLibraSession();
		s.setAttribute(SLAVE_APP_CODE, master2SlaveKey, null);
		s.setSharedAttribute(masterSharedKey, null);
		s.setDisposableAttribute(SLAVE_APP_CODE, master2SlaveDisposableKey,
				null);
		return SUCCESS;
	}

	public String cleanSlaveAttr() throws LibraClientException {
		LibraSession s = LibraUtils.getLibraSession();
		s.setAttribute(MASTER_APP_CODE, slave2MasterValue, null);
		s.setSharedAttribute(slaveSharedValue, null);
		s.setDisposableAttribute(MASTER_APP_CODE, slave2MasterDisposableValue,
				null);
		return SUCCESS;
	}

	public String getAttr() throws LibraClientException {
		getSlaveAttr();
		getMasterAttr();
		return SUCCESS;
	}

	private void getSlaveAttr() throws LibraClientException {
		slave2MasterValue = null;
		slaveSharedValue = null;
		slave2MasterDisposableValue = null;

		LibraSession s = LibraUtils.getLibraSession();

		slave2MasterValue = s.getAttribute(SLAVE_APP_CODE, MASTER_APP_CODE,
				slave2MasterKey);
		slaveSharedValue = s.getSharedAttribute(SLAVE_APP_CODE, slaveSharedKey);
		slave2MasterDisposableValue = s.getDisposableAttribute(SLAVE_APP_CODE,
				MASTER_APP_CODE, slave2MasterDisposableKey);
	}

	private void getMasterAttr() throws LibraClientException {
		master2SlaveValue = null;
		masterSharedValue = null;
		master2SlaveDisposableValue = null;

		LibraSession s = LibraUtils.getLibraSession();

		master2SlaveValue = s.getAttribute(MASTER_APP_CODE, SLAVE_APP_CODE,
				master2SlaveKey);
		masterSharedValue = s.getSharedAttribute(MASTER_APP_CODE,
				masterSharedKey);
		master2SlaveDisposableValue = s.getDisposableAttribute(MASTER_APP_CODE,
				SLAVE_APP_CODE, master2SlaveDisposableKey);
	}

	public String setSlaveAttr() throws LibraClientException {
		System.out.println("fuck youfuck youfuck youfuck youfuck youfuck youfuck youfuck youfuck you");
		LibraSession s = LibraUtils.getLibraSession();
		s.setAttribute(MASTER_APP_CODE, slave2MasterKey, slave2MasterValue);
		s.setSharedAttribute(slaveSharedKey, slaveSharedValue);
		s.setDisposableAttribute(MASTER_APP_CODE, slave2MasterDisposableKey,
				slave2MasterDisposableValue);

		getSlaveAttr();
		return SUCCESS;
	}

	public String setMasterAttr() throws LibraClientException {
		LibraSession s = LibraUtils.getLibraSession();
		s.setAttribute(SLAVE_APP_CODE, master2SlaveKey, master2SlaveValue);
		s.setSharedAttribute(masterSharedKey, masterSharedValue);
		s.setDisposableAttribute(SLAVE_APP_CODE, master2SlaveDisposableKey,
				master2SlaveDisposableValue);

		master2SlaveValue = null;
		masterSharedValue = null;
		master2SlaveDisposableValue = null;

		getMasterAttr();
		return SUCCESS;
	}

	public String getMaster2SlaveKey() {
		return master2SlaveKey;
	}

	public void setMaster2SlaveKey(String master2SlaveKey) {
		this.master2SlaveKey = master2SlaveKey;
	}

	public String getMasterSharedKey() {
		return masterSharedKey;
	}

	public void setMasterSharedKey(String masterSharedKey) {
		this.masterSharedKey = masterSharedKey;
	}

	public String getMaster2SlaveDisposableKey() {
		return master2SlaveDisposableKey;
	}

	public void setMaster2SlaveDisposableKey(String master2SlaveDisposableKey) {
		this.master2SlaveDisposableKey = master2SlaveDisposableKey;
	}

	public String getMaster2SlaveValue() {
		return master2SlaveValue;
	}

	public void setMaster2SlaveValue(String master2SlaveValue) {
		this.master2SlaveValue = master2SlaveValue;
	}

	public String getMasterSharedValue() {
		return masterSharedValue;
	}

	public void setMasterSharedValue(String masterSharedValue) {
		this.masterSharedValue = masterSharedValue;
	}

	public String getMaster2SlaveDisposableValue() {
		return master2SlaveDisposableValue;
	}

	public void setMaster2SlaveDisposableValue(
			String master2SlaveDisposableValue) {
		this.master2SlaveDisposableValue = master2SlaveDisposableValue;
	}

	public String getSlave2MasterKey() {
		return slave2MasterKey;
	}

	public void setSlave2MasterKey(String slave2MasterKey) {
		this.slave2MasterKey = slave2MasterKey;
	}

	public String getSlaveSharedKey() {
		return slaveSharedKey;
	}

	public void setSlaveSharedKey(String slaveSharedKey) {
		this.slaveSharedKey = slaveSharedKey;
	}

	public String getSlave2MasterDisposableKey() {
		return slave2MasterDisposableKey;
	}

	public void setSlave2MasterDisposableKey(String slave2MasterDisposableKey) {
		this.slave2MasterDisposableKey = slave2MasterDisposableKey;
	}

	public String getSlave2MasterValue() {
		return slave2MasterValue;
	}

	public void setSlave2MasterValue(String slave2MasterValue) {
		this.slave2MasterValue = slave2MasterValue;
	}

	public String getSlaveSharedValue() {
		return slaveSharedValue;
	}

	public void setSlaveSharedValue(String slaveShareValue) {
		this.slaveSharedValue = slaveShareValue;
	}

	public String getSlave2MasterDisposableValue() {
		return slave2MasterDisposableValue;
	}

	public void setSlave2MasterDisposableValue(
			String slave2MasterDisposableValue) {
		this.slave2MasterDisposableValue = slave2MasterDisposableValue;
	}

}
